package listerner;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
 * 删除功能监听器
 * @author Little-Grey
 *
 */
public class DeleteListerner implements ActionListener{
	JTextField Tname;
	JDialog dialog;
	public DeleteListerner(JTextField Tname,JDialog dialog) {
		// TODO Auto-generated constructor stub
		this.Tname = Tname;
		this.dialog = dialog;
	}
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		try {
			File file=new File("D:\\AddressBook.txt");
			Scanner find=new Scanner(file);
			FileWriter file1=new FileWriter("D:\\AddressBook1.txt",true);
			PrintWriter write=new PrintWriter(file1);
			while(find.hasNext()){
				String s=find.next();
			if(!(s.equals(Tname.getText()))){
				write.print(s+'\t');
				write.print(find.next()+'\t');
				write.print(find.next()+'\t');
				write.print(find.next()+' ');
				write.print(find.next()+' ');
				write.print(find.next()+'\t');
				write.print(find.next()+'\t');
				write.println(find.next());
				}
			else {
				find.next();
				find.next();
				find.next();
				find.next();
				find.next();
				find.next();
				find.next();
				}
			}
			find.close();
			file.delete();
			write.close();
			file1.close();
			File file2=new File("D:\\AddressBook1.txt");
			file2.renameTo(new File( "D:\\AddressBook.txt"));
			JOptionPane.showMessageDialog(null,"删除成功",
				     "删除结果",JOptionPane.INFORMATION_MESSAGE);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "未找到文件");
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			System.out.print("未找到该人员");
		} 
	}
}
